<?php
namespace ImiApp\WebSocketServer\HttpController;

use Imi\Controller\HttpController;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;

use Imi\Server\Server;
use Imi\Db\Db;
use Imi\Config;

use Yurun\Util\YurunHttp;
use Yurun\Util\HttpRequest;

/**
 * @Controller("/")
 */
class IndexController extends HttpController
{
    /**
     * @Action
     * @Route("/")
     *
     * @return void
     */
    public function index()
    {
        
        // exit('11111111111');
        return [
            'data'  =>  'index',
        ];
    }

    /**
     * @Action
     *
     * @return void
     */
    public function api()
    {
        return [
            'data'  =>  'api',
        ];
    }






    /**
     * @Action
     * 
     * 
     *
     * @return void
     */
    public function sendsubject($content=null,$type='4')
    {
        // $server = Server::getServer(); // 获取当前服务器
        // 发送给所有 Worker

        // /** @var \Imi\Server\Http\Message\Request $request */
        // $request = \Imi\RequestContext::get('request');
        // var_dump($request->getServerParams());

        // var_dump($request->get('token_'));
        
        $db = Db::query();

        // $subject_time = $db->table('fa_config')->where('name','=','subject_time')->select()->get()['value'];


        $subject = $db->table('fa_subject')->where('date','=',date('Y-m-d'))->select()->get();
        var_dump($subject);
        if($subject){
            $content = $subject['name'];
        }else{
            $content = '今日话题无';
        }

        $url = Config::get('@app.domain').'/api/com/get_subject_group';
        // $res = $this->get_http($url);
        $http = new HttpRequest;
        $response = $http->get($url)->body();
        $res_arr = json_decode($response,true);


        $num = 0;
        foreach(explode(',',$res_arr['data']) as $group){
            var_dump($group);
            $user_str = substr($group,12);
            var_dump($user_str);

            $user_arr = explode('_',$user_str);
            
           
            $indata['time'] = time();
            $indata['type'] = '4';

            $indata['send_uid'] = $user_arr[1];
            $indata['send_time'] = time();
            $indata['re_uid'] = $user_arr[0];
            $indata['content'] = $content;
            $indata['group'] = $group;
            // $clientInfo = $this->server->getSwooleServer()->getClientInfo($this->frame->getFd());
            $indata['ip'] = '127.0.0.1';
            $res = $db->table('fa_chat')->insert($indata);

            Server::sendToGroup($group, [
                'action'    =>  'chat',
                'username'    =>  $user->username,
                'send_uid'   =>  $user_arr[1],
                'msg_id'    => $res->getLastInsertId(),
                'type'      =>  '4',
                'content'   =>  $content,
                'image' => '',
                'time'      =>  date('Y-m-d H:i:s'),
            ]);



    
            $indata['send_uid'] = $user_arr[0];

            $indata['re_uid'] = $user_arr[1];

            $res = $db->table('fa_chat')->insert($indata);

            $send_res = Server::sendToGroup($group, [
                'action'    =>  'chat',
                'username'    =>  $user->username,
                'send_uid'   =>  $user_arr[0],
                'msg_id'    => $res->getLastInsertId(),
                'type'      =>  '4',
                'content'   =>  $content,
                'image' => '',
                'time'      =>  date('Y-m-d H:i:s'),
            ]);
            var_dump($send_res);
            if($send_res)
                $num++;
        }


        return $num;

      
        
        // 发送给指定 Worker
        // Server::sendMessageRaw('数据', 0);
    }

    public function get_http($url)
    {
        $http = new HttpRequest;
        $http->protocolVersion = '2.0'; // 这句是关键
        $response = $http->get($url);//->body()
        var_dump($response);
        return $response;
    }


    // /**
    //  * @Action
    //  * 
    //  * @Route("/sendsysmsg")
    //  *
    //  * @return string
    //  */
    // public function sendsysmsg($content=null,$type=null)
    // {
    //     // $server = Server::getServer(); // 获取当前服务器
    //     // 发送给所有 Worker

    //     /** @var \Imi\Server\Http\Message\Request $request */
    //     $request = \Imi\RequestContext::get('request');
    //     var_dump($request->getServerParams());

    //     // var_dump($request->get('token_'));

    //     $db = Db::query();
    //     $indata['send_time'] = time();
    //     $indata['time'] = time();
    //     $indata['type'] = $type;
    //     $indata['admin_id'] = 1;
    //     $indata['content'] = $content;

    //     // $clientInfo = $this->server->getSwooleServer()->getClientInfo($this->frame->getFd());
    //     $indata['ip'] = $request->getServerParams('remote_addr');
    //     $db->table('sc_jigou_msg')->insert($indata);

    //     // var_dump($indata);

    //     $num = Server::sendToAll([
    //         'type'  => $type,
    //         'data'  =>  $content,
    //         'msg'  =>  'all',
    //         'content'=>$content,
    //         'send_id'=>'1',
    //         'time'=>date('Y-m-d H:i:s'),
    //     ]);


    //     return $num;

      
        
    //     // 发送给指定 Worker
    //     // Server::sendMessageRaw('数据', 0);
    // }



    /**
     * @Action
     * 
     * @Route("/sendsysmsg")
     *
     * @return string
     */
    public function sendsysmsg($content=null,$type=null)
    {
        // $server = Server::getServer(); // 获取当前服务器
        // 发送给所有 Worker

        /** @var \Imi\Server\Http\Message\Request $request */
        $request = \Imi\RequestContext::get('request');
        var_dump($request->getServerParams());

        // var_dump($request->get('token_'));

        $db = Db::query();
        $indata['send_time'] = time();
        $indata['time'] = time();
        $indata['type'] = $type;
        $indata['admin_id'] = 1;
        $indata['content'] = $content;

        // $clientInfo = $this->server->getSwooleServer()->getClientInfo($this->frame->getFd());
        $indata['ip'] = $request->getServerParams('remote_addr');
        $db->table('sc_jigou_msg')->insert($indata);

        // var_dump($indata);

        $num = Server::sendToAll([
            'type'  => $type,
            'data'  =>  $content,
            'msg'  =>  'all',
            'content'=>$content,
            'send_id'=>'1',
            'time'=>date('Y-m-d H:i:s'),
        ]);


        return $num;

      
        
        // 发送给指定 Worker
        // Server::sendMessageRaw('数据', 0);
    }



    /**
     * @Action
     * 
     * @Route("/sendsysmsgid")
     *
     * @return string
     */
    public function sendsysmsgid($id)
    {
        // $server = Server::getServer(); // 获取当前服务器
        // 发送给所有 Worker

        /** @var \Imi\Server\Http\Message\Request $request */
        $request = \Imi\RequestContext::get('request');
        var_dump($request->getServerParams());

        // var_dump($request->get('token_'));

        $db = Db::query();
        $data = $db->table('sc_jigou_msg')->where('id','=',$id)->select();
        var_dump($data);
       

        $indata['ip'] = $request->getServerParams('remote_addr');
        // $db->table('sc_jigou_msg')->insert($indata);

        // var_dump($indata);

        $num = Server::sendToAll([
            'type'  => $data['type'],
            'data'  =>  $content,
            'msg'  =>  'all',
            'content'=>$content,
            'send_id'=>'1',
            'time'=>date('Y-m-d H:i:s'),
        ]);


        return $num;

      
        
        // 发送给指定 Worker
        // Server::sendMessageRaw('数据', 0);
    }

}
